#include <iostream>
#include <vector>
#include <climits>
#include <algorithm>
typedef long long LL;
using namespace std;
int n;
vector<LL> a(1001);
LL res = INT_MAX;
LL gap(LL a, LL b){
    return a > b ? a-b : b-a;
}
int main()
{
    // 请在此输入您的代码
    cin >> n;
    for(int i = 0; i < n ; ++i){
        cin >> a[i];
    }
    for(int l = 0; l < n ; ++l){
        for(int r = l + 1; r < n; ++r ){
            res = min(res,gap(a[l],a[r]));
            int ll = l - 1;
            int rr = r + 1;
            LL lsum = a[l];
            LL rsum = a[r];
            while(ll >= 0 && rr < n){
                if(lsum < rsum){
                    lsum += a[ll--];
                }else{
                    rsum += a[rr++];
                }
                res = min(res,gap(lsum,rsum));
            }
            while(ll>=0){
                lsum += a[ll--];
                res = min(res,gap(lsum,rsum));
            }
            while(rr<n){
                rsum += a[rr++];
                res = min(res,gap(lsum,rsum));
            }
        }
    }
    cout << res;
    return 0;
}